package net.sk.china.city.observer;

import net.sk.china.city.mapper.ArRegionMapper;
import net.sk.china.city.observer.event.volunteer.VolunteerDecrEvent;
import net.sk.china.city.observer.event.volunteer.VolunteerIncrEvent;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
public class RegionObserver {


    private final ArRegionMapper arRegionMapper;

    @EventListener
    public void listenRegionEvent(VolunteerIncrEvent volunteerIncrEvent) {
        String regionIdList = volunteerIncrEvent.getRegionIdList();
        if (StringUtils.isEmpty(regionIdList)) {
            return;
        }
        String[] regionIdArray = regionIdList.split(",");
        arRegionMapper.incrVolunteerCount(regionIdArray);
    }

    @EventListener
    public void listenRegionEvent(VolunteerDecrEvent volunteerIncrEvent) {
        String regionIdList = volunteerIncrEvent.getRegionIdList();
        if (StringUtils.isEmpty(regionIdList)) {
            return;
        }
        String[] regionIdArray = regionIdList.split(",");
        arRegionMapper.decrVolunteerCount(regionIdArray);
    }
}
